package com.power.sifa.respond.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.respond.api.IApl_organFeign;
import com.power.sifa.respond.model.Apl_organ;
import com.power.sifa.respond.model.pageInfo.Apl_organPageInfo;
import com.power.sifa.respond.service.Apl_organService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 *   应诉机关控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_organController",tags={"应诉机关"})
public class Apl_organController implements IApl_organFeign {

    @Resource
    private Apl_organService apl_organService;


    @Override
    @ApiOperation("查询应诉机关分页列表")
    @SaCheckPermission("apl_organ_list")
    public ResponseDTO list(Apl_organPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_organ> page = this.apl_organService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_organ apl_organ : page) {
                apl_organ.geneText();
                list.add(apl_organ);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看应诉机关")
    @SaCheckPermission("apl_organ_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_organ apl_organ  = apl_organService.getById(id);
        if (apl_organ != null) {
            apl_organ.geneText();
        }
        return ResponseDTO.succData(apl_organ);
    }

    @Override
    @ApiOperation("删除应诉机关")
    @SaCheckPermission("apl_organ_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_organService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改应诉机关")
    @SaCheckPermission("apl_organ_save")
    public ResponseDTO save(Apl_organ apl_organ) {
        Assert.notNull(apl_organ, "apl_organ不能为空");
        apl_organService.saveOrUpdate(apl_organ);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除应诉机关")
    @SaCheckPermission("apl_organ_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_organService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_organ_list")
    public void output(HttpServletResponse response, Apl_organPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "应诉机关";
        String[] heads = {"applyid+nanoid","applyid","顺序号","应诉机关类型","应诉机关级别","机关名称","是否委托代理人","代理人类型","其他"};
        String[] fields = {"id","applyid","sortnum","type","level","name","isagent","agenttype","other"};
        Page<Apl_organ> page = this.apl_organService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "应诉机关";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"applyid+nanoid","applyid","顺序号","应诉机关类型","应诉机关级别","机关名称","是否委托代理人","代理人类型","其他"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","applyid","sortnum","type","level","name","isagent","agenttype","other"};
        List<Apl_organ> list = (List<Apl_organ>)ExcelUtil.Import(fields, file, Apl_organ.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_organService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

}
